#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>
// #include "oj_model_file.hpp"
#include "oj_model_mysql.hpp"

namespace ns_view
{
    using namespace ns_model;
    const std::string template_all_path = "./template_html/all_questions.html";
    const std::string template_one_path = "./template_html/one_question.html";
    class View
    {
    public:
        View()
        {}
        ~View()
        {}
        void AllExpandHtml(const std::vector<Question> &questions,std::string &out)
        {
            //一张题目列表
            //题目编号 题目名称 题目难度
            std::string src_html = template_all_path;
            
            //1. 数据字典
            ctemplate::TemplateDictionary root("all_questions");
            //构建相应子字典，能够完成循环结构
            for(const auto &e : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number",e.number);
                sub->SetValue("title",e.title);
                sub->SetValue("star",e.star);            
            }

            //2.获取html网页模板内容
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            //3.开始进行渲染
            tpl->Expand(&out,&root);
        }
        
        void OneExpandHtml(const Question &q,std::string &out)
        {
            std::string src_html = template_one_path;

            //1.数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("pre_code",q.header);
            root.SetValue("desc",q.desc);

            //2.获取网页模板
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            //3.进行渲染
            tpl->Expand(&out,&root);
        }

        //获取OJ首页内容
        bool GetHomePage(std::string &out)
        {
            std::string root = webroot + "index.html";
            out = FileUtil::ReadFile(root);
            if(out.size())
                return true;
            return false;
        }
    };
}
